//菜单相关。。另外可个性化菜单
//个性可根据标签或性别或手机系统或地区或语音（用于区分国家），这几项显示不同的菜单
const config=require('./config')
const Wechat=require('./vchat').Wechat
const prefix='https://api.weixin.qq.com/cgi-bin/';
const api={
    newMenu:prefix+'menu/create?',  //创建
    delMenu:prefix+'menu/delete?',  //删除
    findMenu:prefix+'get_current_selfmenu_info?'
}

let chat=new Wechat(config.wechat)
exports.menu={
    menuOpt:async (dataType) => {
        let url=''
        let postData=null
        switch(dataType){
            case 'newMenu':
                url=api.newMenu;
                postData={
                    "button":[
                        // {   //最多只能3个一级菜单
                        //     "type":"click",  //点击事件
                        //     "name":"歌曲",
                        //     "key":"sing"
                        // },
                        {
                            "name":"菜单",
                            "sub_button":[   //二级菜单
                                {
                                    "type":"view",  //跳转事件
                                    "name":"网站",
                                    "url":"http://err.free-http.svipss.top/dy"
                                },
                                // {
                                //     "type":"miniprogram",
                                //     "name":"wxa",  //小程序名
                                //     "url":"http://mp.weixin.qq.com",
                                //     "appid":"wx286b93c14bbf93aa",
                                //     "pagepath":"pages/lunar/index"
                                // },
                                {
                                    "type": "view_limited",
                                    "name": "测试图文消息",
                                    "media_id": "b4vZdtElHrFMIPy8N2ezJZgH5lKJhz3jSU5hHHfEu6w"
                                },
                                {
                                    "name": "发送位置",
                                    "type": "location_select",
                                    "key": "rselfmenu_2_0"
                                },
                                {
                                    "type": "media_id",
                                    "name": "图片",
                                    "media_id": "b4vZdtElHrFMIPy8N2ezJRcGCY_nS5BGYuA0ky5T6b4"
                                }
                               ]
                        },
                        {
                            "name": "扫码",
                            "sub_button": [
                                {
                                    "type": "scancode_waitmsg",
                                    "name": "扫码带提示",
                                    "key": "rselfmenu_01",
                                    "sub_button": [ ]
                                },
                                {
                                    "type": "scancode_push",
                                    "name": "扫码推事件",
                                    "key": "rselfmenu_02",
                                    "sub_button": [ ]
                                }
                            ]
                        },
                        {
                            "name": "发图",
                            "sub_button": [
                                {
                                    "type": "pic_sysphoto",
                                    "name": "系统拍照发图",
                                    "key": "rselfmenu_1_0",
                                    "sub_button": [ ]
                                },
                                {
                                    "type": "pic_photo_or_album",
                                    "name": "拍照或者相册发图",
                                    "key": "rselfmenu_1_1",
                                    "sub_button": [ ]
                                },
                                {
                                    "type": "pic_weixin",
                                    "name": "微信相册发图",
                                    "key": "rselfmenu_1_2",
                                    "sub_button": [ ]
                                }
                            ]
                        }
                    ]
                }
                break;
            case 'delMenu':
                url=api.delMenu
                break;
            case 'findMenu':
                url=api.findMenu
                break;

        }
        return chat.getAcc().then(data=>{
            url+=`access_token=${data.access_token}`;
            return chat.getBody({url:url,method:'POST',body:postData,json:true})
        })
    }
}
